<?php
/**
 * Created by PhpStorm.
 * User: USER
 * Date: 2017/4/25
 * Time: 19:55
 */

namespace JsonRpc\Services\System;

use JsonRpc\Models\SysPermission;
use JsonRpc\Services\CommonService;


class PowerService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\SysPermission";
    }

    public static function getPowerList($params)
    {
        $res = self::getInfoList($params);
        if (!empty($res)) {
            return $res;
        }
        return [];
    }

    //获取单个权限编码
    public static function getPowerOne($params)
    {
        $res = SysPermission::where('id',$params['id'])->first();
        if (!empty($res)) {
            return $res;
        }
        return [];
    }

    //保存权限编码
    public static function savePowerInfo($params)
    {
        if(isset($params['id']) && $params['id']){
            //更新
            $model = SysPermission::find($params['id']);
            $model->updated_by = $params['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new SysPermission();
            $model->client_id = $user['client_id'];
            $model->org_id = $user['org_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }
        $model->code = $params['code'];
        $model->name = $params['name'];
        $model->description = $params['description'];
        return $model->save();
    }

    public static function removePower($params)
    {
        if (!empty($params['id'])) {
            $res = SysPermission::where('id',$params['id'])->update([
                'is_active' => '0'
            ]);
            return $res;
        }
        return [];
    }
}